// This is a part of the WceUi library.
// Copyright (C) WceUi Team
// All rights reserved.
//
// Coded by Hui Xu
//
#include "StdAfx.h"
#include "WceUiButton.h"

#pragma warning(disable:4996)

CWceUiButton::CWceUiButton()
{
	m_pNoramlStyleName[0] = '\0';
	m_pDownStyleName[0] = '\0';
	m_pFocusStyleName[0] = '\0';
	m_bMouseDown = FALSE;
	m_Status = WCEUI_BUTTON_NORMAL;
}

CWceUiButton::~CWceUiButton()
{
	
}


void CWceUiButton::SetProperties(PCWceUiXmlElem pCreateElem)
{
	ASSERT(NULL != pCreateElem);
	CWceUiLayer::SetProperties(pCreateElem);

	LPCTSTR pNormalStyleName = pCreateElem->GetAttr(L"style");
	LPCTSTR pDownStyleName = pCreateElem->GetAttr(_T("down.style"));
	LPCTSTR pFocusStyleName = pCreateElem->GetAttr(_T("focus.style"));

	if (NULL != pNormalStyleName)
	{
		SetNormalStyleName(pNormalStyleName);
	}

	if (NULL != pDownStyleName)
	{
		SetDownStyleName(pDownStyleName);
	}

	if (NULL != pFocusStyleName)
	{
		SetFocusStyleName(pFocusStyleName);
	}
}

void CWceUiButton::OnLButtonDown(UINT nFlags,  POINT point)
{
	UNREFERENCED_PARAMETER(nFlags);
	UNREFERENCED_PARAMETER(point);

	ASSERT(!m_bMouseDown);
	m_bMouseDown = TRUE;

	SetStatus(WCEUI_BUTTON_DOWN);

	SetCapture();	
}

void CWceUiButton::OnLButtonUp(UINT nFlags,	 POINT point)
{
	UNREFERENCED_PARAMETER(nFlags);
	if (m_bMouseDown)
	{
		m_bMouseDown = FALSE;

		SetStatus(WCEUI_BUTTON_NORMAL);
		ReleaseCapture();

		RECT rcWnd;
		GetWindowRect(&rcWnd);
		if (::PtInRect(&rcWnd, point))
		{
			GetCmdReceiver()->OnClick(this);
		}
	}
}

void CWceUiButton::OnMouseMove(UINT nFlags,	 POINT point)
{
	UNREFERENCED_PARAMETER(nFlags);
	if (m_bMouseDown)
	{
		RECT rcWnd;
		GetWindowRect(&rcWnd);
		if (!::PtInRect(&rcWnd, point))
		{
			SetStatus(WCEUI_BUTTON_FOCUS);
		}else
		{
			SetStatus(WCEUI_BUTTON_DOWN);
		}
	}
}

void CWceUiButton::OnLoseCapture()
{
	POINT pt;
	pt.x = -USHRT_MAX;
	pt.y = -USHRT_MAX;
	OnLButtonUp(0, pt);
}

LPCTSTR CWceUiButton::GetDownStyleName() const
{
	return m_pDownStyleName;
}

void CWceUiButton::SetDownStyleName(LPCTSTR pDownStyleName)
{
	ASSERT(NULL != pDownStyleName);

	m_pDownStyleName[MAX_TAG_NAME_LEN - 1] = '\0';
	_tcsncpy(m_pDownStyleName, pDownStyleName, MAX_TAG_NAME_LEN - 1);
}

LPCTSTR CWceUiButton::GetFocusStyleName() const
{
	return m_pFocusStyleName;
}

void CWceUiButton::SetFocusStyleName(LPCTSTR pFocusStyleName)
{
	ASSERT(NULL != pFocusStyleName);

	m_pFocusStyleName[MAX_TAG_NAME_LEN - 1] = '\0';
	_tcsncpy(m_pFocusStyleName, pFocusStyleName, MAX_TAG_NAME_LEN - 1);
}

LPCTSTR CWceUiButton::GetNoramlStyleName() const
{
	return m_pNoramlStyleName;
}

void CWceUiButton::SetNormalStyleName(LPCTSTR pNoramlStyleName)
{
	ASSERT(NULL != pNoramlStyleName);

	m_pNoramlStyleName[MAX_TAG_NAME_LEN - 1] = '\0';
	_tcsncpy(m_pNoramlStyleName, pNoramlStyleName, MAX_TAG_NAME_LEN - 1);	
}

void CWceUiButton::SetStatus(WCEUI_BUTTON_STATUS status)
{
	switch(m_Status)
	{
	case WCEUI_BUTTON_NORMAL:
		{
			switch(status)
			{
			case WCEUI_BUTTON_DOWN:  //normal -> down
				{
					if (*m_pDownStyleName)
					{
						SetStyle(m_pDownStyleName);
					}
				}
				m_Status = status;
				break;
			case WCEUI_BUTTON_FOCUS: //normal -> focus is not valid
				ASSERT(FALSE);
				break;
			case WCEUI_BUTTON_NORMAL:
				break;
			default:
				ASSERT(FALSE);
				break;
			}
		}
		break;
	case WCEUI_BUTTON_DOWN:
		{
			switch(status)
			{
			case WCEUI_BUTTON_DOWN:
				break;
			case WCEUI_BUTTON_FOCUS: //down -> focus
				{
					if (*m_pFocusStyleName)
					{
						SetStyle(m_pFocusStyleName);
					}
				}
				m_Status = status;
				break;
			case WCEUI_BUTTON_NORMAL: //down -> normal
				SetStyle(m_pNoramlStyleName);
				m_Status = status;
				break;
			default:
				ASSERT(FALSE);
				break;
			}
		}
		break;
	case WCEUI_BUTTON_FOCUS:
		{
			switch(status)
			{
			case WCEUI_BUTTON_DOWN: //focus -> down
				{
					if (*m_pDownStyleName)
					{
						SetStyle(m_pDownStyleName);
					}else
					{
						SetStyle(m_pNoramlStyleName);
					}
				}
				m_Status = status;				
				break;
			case WCEUI_BUTTON_FOCUS:
				break;
			case WCEUI_BUTTON_NORMAL:
				SetStyle(m_pNoramlStyleName);
				m_Status = status;
				break;
			default:
				ASSERT(FALSE);
				break;
			}
		}
		break;
	default:
		ASSERT(FALSE);
		break;
	}
}